package com.ukao.pad.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.ukao.pad.R;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.rfid.UserInfoEvent;
import com.ukao.pad.widget.StateImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 修改名字
 * Created by ukaodev on 2017/6/2.
 */

public class EditNameDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.edit_name_et)
    EditText mNameEt;
    @BindView(R.id.edit_name_clean_btn)
    Button mCleanBtn;
    @BindView(R.id.edit_name_cancel)
    Button meCancel;
    @BindView(R.id.edit_name_submit)
    Button mSubmit;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.tv_title)
    TextView title;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_edit_name, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        String name = bundle.getString("name", "");
        mNameEt.setText(name);
        //光标移到最后
        mNameEt.setSelection(mNameEt.getText().length());
        title.setText("修改名字");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.edit_name_clean_btn, R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_name_clean_btn:
                mNameEt.setText("");
                break;
            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.edit_name_submit:
                hideKeyboard();
                UserInfoEvent.postHasData(UserInfoEvent.Message.NAME, mNameEt.getText().toString());
                dismiss();
                break;
        }
    }

}
